<!--菜单-->
<!--chenyi 2019-09-26 14:37:29-->
<template>
  <div>
    <!--搜索条件-->
    <el-form inline :model="searchParam" ref="searchParam" label-width="80px" class="search-form">
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="searchParam.name" placeholder="菜单名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <es-select v-model="searchParam.type" enum-name="MenuTypeEnum"></es-select>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentId">
        <es-tree v-model="searchParam.parentId" expand-root  url="/admin/menu/treeData"></es-tree>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchForm">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetSearchForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="edit">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="handleDelete">删除</el-button>
    </el-row>
    <!--表格-->
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      element-loading-text="数据加载中"
      @selection-change="handleSelectionChange"
      class="mt-20">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="parentName"
        label="上级菜单"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{scope.row.parentName||'顶级菜单'}}
        </template>
      </el-table-column>

      <el-table-column
        prop="name"
        label="菜单名称"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="perms"
        label="权限"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="type"
        label="类型"
        width="100"
      >
        <template slot-scope="scope">
          <es-value-format enum-name="MenuTypeEnum" :value="scope.row.type"
                          render-color></es-value-format>
        </template>
      </el-table-column>

      <el-table-column
        prop="icon"
        label="菜单图标"
        show-overflow-tooltip
        width="100">
        <template slot-scope="scope">
          <span :class="scope.row.icon" style="font-size: 25px;color: #409EFF;"></span>
        </template>
      </el-table-column>

      <el-table-column
        prop="orderNum"
        label="排序"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="route"
        label="路由route"
        width="300"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="status"
        label="状态"
        width="150">
        <template slot-scope="scope">
          <!--自定义组件 字典转义-->
          <es-value-format
            enum-name="StatusEnum"
            :value="scope.row.status"
           render-color>

          </es-value-format>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        show-overflow-tooltip
        width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="detail(scope.row)">查看
          </el-button>
          <el-button
            size="mini" type="primary"
            @click="edit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            v-if="scope.row.isSys == 0"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      style="margin: 20px;float: right;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="searchParam.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--新增修改-->
    <el-dialog :title="title" :visible="action==='add' || action==='edit'" :before-close="handleClose">
      <el-form :model="item" :rules="rules" ref="ruleForm" label-width="100px">
        <div class="form-item-group">
          <el-form-item label="菜单类型" prop="type">
            <es-select v-model="item.type" enum-name="MenuTypeEnum" tips="false"></es-select>
          </el-form-item>

          <el-form-item label="菜单名称" prop="name">
            <el-input v-model="item.name"
                      placeholder="请输入菜单名称"
                      maxlength="50"
                      show-word-limit
            ></el-input>
          </el-form-item>

          <el-form-item label="上级菜单" prop="parentId">
            <es-tree v-model="item.parentId" expand-root url="/admin/menu/treeData"></es-tree>
          </el-form-item>

          <el-form-item label="菜单路由" prop="route" v-if="item.type!=3">
            <el-input v-model="item.route"
                      placeholder="请输入菜单路由"
                      maxlength="50"
                      show-word-limit
            ></el-input>
          </el-form-item>

          <el-form-item label="授权标识" prop="perms" v-show="item.type!=1">
            <el-input v-model="item.perms"
                      placeholder="多个权限用逗号隔开"
                      maxlength="200"
                      show-word-limit
            ></el-input>
          </el-form-item>

          <el-form-item label="图标" prop="icon" v-show="item.type==1 || item.type==2">
            <el-input v-model="item.icon"
                      placeholder="请输入图标"
                      maxlength="50"
                      show-word-limit
            >
              <el-button slot="append" icon="el-icon-search" @click="openFont"></el-button>
            </el-input>
          </el-form-item>

          <el-form-item label="排序号" prop="orderNum">
            <el-input v-model="item.orderNum"
                      placeholder="请输入排序号"
                      maxlength="8"
                      show-word-limit
            ></el-input>
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <es-select v-model="item.status" enum-name="StatusEnum"></es-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="action = 'list'">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--详情页-->
    <el-dialog :title="title" :visible="action==='info'" :before-close="handleClose">
      <el-form :model="item" label-width="100px">
        <div class="form-item-group">

          <el-form-item label="菜单类型:" prop="type">
            <es-value-format :value="item.type" enum-name="MenuTypeEnum" ></es-value-format>
          </el-form-item>

          <el-form-item label="菜单名称:" prop="name" >
            <span>{{item.name||"-"}}</span>
          </el-form-item>

          <el-form-item label="上级菜单:" >
            <span>{{item.parentName||"顶级菜单"}}</span>
          </el-form-item>

          <el-form-item label="菜单路由:" prop="route" >
            <span>{{item.route||"-"}}</span>
          </el-form-item>

          <el-form-item label="授权标识:" prop="perms" >
            <span>{{item.perms||"-"}}</span>
          </el-form-item>

          <el-form-item label="图标:" prop="icon" >
            <i :class="item.icon"></i>
          </el-form-item>

          <el-form-item label="排序号:" prop="orderNum">
            <span>{{item.orderNum||"-"}}</span>
          </el-form-item>

          <el-form-item label="状态:" prop="status">
            <es-value-format :value="item.status" enum-name="StatusEnum"></es-value-format>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="action = 'list'">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getList, save, update, remove} from '@/api/sys/menuApi'
  import validate from '@/libs/validate'

  export default {
    name: 'menuList',
    data () {
      return {
        searchParam: {//表格数据请求参数
          page: 1,
          limit: 10
        },
        title: '',
        action: 'list',
        loading: false,
        item: {},  //修改/详情数据
        rules: {  //表单验证
          module: [
            {required: true, message: '请选择菜单模块', trigger: 'change'}
          ],
          type: [
            {required: true, message: '请选择菜单类型', trigger: 'change'}
          ],
          name: [
            {required: true, message: '请输入菜单名称', trigger: 'blur'},
          ],
          parentId: [
            {required: true, message: '请选择上级菜单', trigger: 'change'},
          ],
          route: [
            {required: true, message: '请输入菜单路由', trigger: 'blur'}
          ],
          orderNum: [
            {required: true, message: '请输入排序', trigger: 'blur'},
            {validator: validate.integer, trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ]
        },
        multipleSelection: [], //复选框选中的值
        tableData: [],
        total: 0,
        ids: [],
        menuMap:{},
        menuTree:[],
      }
    },
    created () {

    },
    mounted () {
      //获取数据
      this.getTableData();
    },
    methods: {
      //获取数据
      getTableData () {
        let _this = this
        _this.loading = true
        getList(_this.searchParam).then((res) => {
          if (res.code === 200) {
            _this.tableData = res.data
            console.log(_this.tableData)
            _this.total = res.total
          } else {
            this.$message.error(res.msg)
          }
          _this.loading = false
        })
      },
      //提交表单
      submitForm () {
        let _this = this
        _this.$refs['ruleForm'].validate(function (valid) {
          if (valid) {
            //保存
            if (_this.action === 'add') {
              save(_this.item).then((res) => {
                if (res.code === 200) {
                  _this.$message.success('新增成功！')
                  _this.getTableData()
                } else {
                  _this.$message.error(res.msg)
                }
                _this.action = 'list'
              })
            }
            //修改
            else if (_this.action === 'edit') {
              update(_this.item).then((res) => {
                if (res.code === 200) {
                  _this.$message.success('修改成功！')
                  _this.getTableData()
                } else {
                  _this.$message.error(res.msg)
                }
                _this.action = 'list'
              })
            }
          }
        })
      },
      //删除
      handleDelete: function (index, row) {
        let temp = Object.assign({}, row)
        //获取选中的id
        let ids = this.getSelectIds(temp)
        if (ids.length > 0) {
          this.ids = ids
          //删除数据
          let _this = this
          _this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            callback: function (action, instance) {
              if (action === 'confirm') {
                remove(_this.ids).then((res) => {
                  if (res.code === 200) {
                    //刷新表格数据
                    _this.getTableData()
                    _this.$message.success('删除成功!')
                  } else {
                    _this.$message.error(res.msg)
                  }
                })

              }
            }
          })
        }
      },
      openFont: function () {
        window.open('http://fontawesome.dashgame.com/')
      },
      add () {
        this.item = {}
        this.action = 'add'
        this.title = '新增菜单'
      },
      //修改页面
      edit: function (index, row) {
        let _this = this
        let temp = Object.assign({}, row)
        if (Object.keys(temp).length === 0) {
          let chcekeds = _this.multipleSelection
          if (chcekeds.length === 0) {
            _this.$message.error('请选择一条数据')
            return false
          } else if (chcekeds.length > 1) {
            _this.$message.error('只能选择一条数据')
            return false
          } else {
            _this.item = chcekeds[0]
          }
        } else {
          _this.item = temp
        }
        _this.action = 'edit'
        _this.title = '修改菜单'
        _this.currentIndex = index
      },
      detail(row){
        this.item = row;
        this.action='info';
      },
      handleClose (done) {
        this.action = 'list'
        done()
      },
      //获取选中行
      getSelectIds: function (row) {
        let chcekeds = []
        let ids = []
        if (!row || Object.keys(row).length === 0) {
          chcekeds = this.multipleSelection
          if (chcekeds.length === 0) {
            this.$message.error('至少选择一条数据')
          } else {
            for (let key in chcekeds) {
              ids.push(chcekeds[key].menuId)
            }
          }
        } else {
          ids.push(row.menuId)
        }
        return ids

      },
      //查询表格
      searchForm () {
        this.searchParam.page = 1
        //获取数据
        this.getTableData()
      },
      //重置查询表格
      resetSearchForm () {
        this.$refs['searchParam'].resetFields()
        //获取数据
        this.getTableData()
      },
      handleSizeChange (limit) {
        this.searchParam.limit = limit
        //获取数据
        this.getTableData()
      },
      handleCurrentChange (page) {
        this.searchParam.page = page
        //获取数据
        this.getTableData()
      },
      //复选框选择
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
    }
  }
</script>

<style scoped>

</style>
